package com.jungle.form;

import com.dlsc.formsfx.model.structure.*;
import com.dlsc.formsfx.model.validators.StringLengthValidator;
import com.dlsc.formsfx.model.validators.IntegerRangeValidator;
import com.dlsc.formsfx.view.renderer.FormRenderer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.Arrays;

public class FormsFXExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // 创建表单模型
        Form form = Form.of(
                Section.of(
                        Field.ofStringType("")
                                .label("Name")
                                .placeholder("Enter your name")
                                .validate(StringLengthValidator.atLeast(3, "Name must be at least 3 characters long")),

                        Field.ofStringType("")
                                .label("Email")
                                .placeholder("Enter your email")
                                .validate(StringLengthValidator.between(5, 50, "Email must be between 5 and 50 characters long")),

                        Field.ofIntegerType(0)
                                .label("Age")
                                .placeholder("Enter your age")
                                .validate(IntegerRangeValidator.between(18, 100, "Age must be between 18 and 100")),

                        Field.ofBooleanType(false)
                                .label("boolean")
                                .placeholder("").required(true),
                        Field.ofSingleSelectionType(Arrays.asList("a", "b", "c"),0)
                                .label("Single Selection"),
                        Field.ofMultiSelectionType(Arrays.asList("aa","Bb","CC")).label("Multi Selection")
                ).title("sssssssss")
        ).title("Registration Form");

        // 创建表单视图
        FormRenderer formRenderer = new FormRenderer(form);
        // 设置提交按钮的行为
//        form.setOnSubmit(event -> {
//            if (form.isValid()) {
//                System.out.println("Form submitted successfully!");
//                System.out.println("Name: " + form.getFields().get(0).getValue());
//                System.out.println("Email: " + form.getFields().get(1).getValue());
//                System.out.println("Age: " + form.getFields().get(2).getValue());
//            } else {
//                System.out.println("Form contains errors. Please fix them.");
//            }
//        });

        // 设置场景并显示窗口
        StackPane root = new StackPane(formRenderer);
        Scene scene = new Scene(root, 400, 300);
        primaryStage.setTitle("JavaFX FormsFX Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}